001    /* $RCSfile: RSAKeyPairGeneratorEngine.java,v $
002     * $Revision: 1.10 $
003     * $Date: 2003/10/04 19:18:38 $
004     * $Author: uwe_guenther $
005     * $State: Exp $
006     *
007     * Created on November 8, 2001 11:25 AM
008     *
009     * Copyright (C) 2001  Uwe Guenther  <uwe@cscc.de >
010     *
011     * This file is part of the jhbci JCE-ServiceProvider. The jhbci JCE-
012     * ServiceProvider is a library, written in JavaTM, that should be 
013     * used in HBCI banking applications (clients and may be servers),
014     * to do cryptographic operations.
015     *
016     * The jhbci library is free software; you can redistribute it and/or
017     * modify it under the terms of the GNU Lesser General Public
018     * License as published by the Free Software Foundation; either
019     * version 2.1 of the License, or (at your option) any later version.
020     *
021     * The jhbci library is distributed in the hope that it will be useful,
022     * but WITHOUT ANY WARRANTY; without even the implied warranty of
023     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
024     * Lesser General Public License for more details.
025     *
026     * You should have received a copy of the GNU Lesser General Public
027     * License along with this library; if not, write to the Free Software
028     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
029     *
030     */
031    
032    package de.cscc.crypto.provider;
033    
034    import java.security.InvalidAlgorithmParameterException;
035    import java.security.KeyPair;
036    import java.security.KeyPairGeneratorSpi;
037    import java.security.SecureRandom;
038    import java.security.spec.AlgorithmParameterSpec;
039    
040    /** 
041     * RSAKeyPairGeneratorEngine Class.
042     *
043     * <p>Defaults are:
044     * <ol>
045     * <li>public Exponent = F4 (Forth Fermat number (2^2^4)+1 = 65537 or 
046     *     0x00010001)
047     * <li><code>keysize</code> (modulus length) = 768 bit
048     *     <br><b>Note:</b> The keysize must be a positive multiple of 
049     *     256 and equals or greater than 768.</li>
050     * <li><code>random = new java.security.SecureRandom()</code></li>
051     * </ol>
052     *
053     * @author  <a href=mailto:uwe@cscc.de >Uwe Günther </a>
054     *
055     * @version $Revision: 1.10 $
056     */
057    public final class RSAKeyPairGeneratorEngine extends KeyPairGeneratorSpi {
058        
059        /** The delegate for this wrapper object */
060        private RSAKeyPairGeneratorImpl generator = new RSAKeyPairGeneratorImpl();
061        
062        /** Creates new RSAKeyPairGeneratorEngine*/
063        public RSAKeyPairGeneratorEngine() {
064            if (JHBCI.selfIntegrityChecking() == false) {
065                throw new SecurityException("JHBCI-Provider is tampered.");
066            }        
067        }
068    
069        /**
070         * Returns a string representation of the object. 
071         *
072         * @return  a string representation of the object.
073         */
074        public String toString() {
075            return this.generator.toString();
076        }    
077        
078        /**
079         * Initializes the key pair generator for a certain keysize, using
080         * the default parameter set. If random is <code>null</code> we use
081         * the <code>SecureRandom</code> implementation of the
082         * highest-priority installed provider as the source of randomness.
083         * (If none of the installed providers supply an implementation of
084         * <code>SecureRandom</code>, a system-provided source of randomness
085         * is used.) 
086         * <p><b>Note:</b> If there a reinitialization with a <code>null</code>
087         * reference for <code>random</code> we use the existing random object,
088         * that was valid before reinitialization.
089         *
090         * @param keysize the keysize. The keysize must be a positive multiple of 
091         * 256 and equals or greater than 768.
092         * @param random the source of randomness for this generator. If random is 
093         * <code>null</code> we use the the <code>SecureRandom</code> implementation
094         * of the highest-priority installed provider as the source of randomness.
095         * (If none of the installed providers supply an implementation of
096         * <code>SecureRandom</code>, a system-provided source of randomness
097         * is used.)
098         * @throws InvalidParameterException if the <code>keysize</code> is not
099         * equals or geater than 768 and a multible of 256. Where the unit is bit.
100         */
101        public void initialize(int keysize, SecureRandom random) {
102            this.generator.initialize(keysize, random);
103        }
104        
105        /**
106         * Initializes the key pair generator using the specified parameter
107         * set and user-provided source of randomness.
108         *
109         * You have to use {@link java.security.spec.RSAKeyGenParameterSpec} as 
110         * <code>AlgorithmParamterSpec</code>. <code>RSAKeyGenParameterSpec</code>
111         * does support <code>int keysize</code> and 
112         * <code>BigInteger publicExponent</code>. The <code>keysize</code> must be 
113         * 768 or greater and a multible of 256. 
114         *
115         * <p>So that <code>keysize</code> satisfies the equation: 
116         * <ul>
117         * <li><code>keysize = x * 256, where x = 3, 4, 5, 6, ...</code></li>
118         * </ul>
119         * The possible values for <code>publicExponent</code> are:
120         * <ul>
121         * <li><code>publicExponent = </code>
122         * {@link java.security.spec.RSAKeyGenParameterSpec#F0} = 3</li>
123         * <li><code>publicExponent = </code>
124         * {@link java.security.spec.RSAKeyGenParameterSpec#F4} = 65537</li>
125         * </ul>
126         *
127         * <p><b>Note:</b> If there a reinitialization with a <code>null</code>
128         * reference for <code>random</code> we use the existing random object,
129         * that was valid before reinitialization.
130         *
131         * @param params the parameter set used to generate the keys.
132         * @param random the source of randomness for this generator.
133         * @throws InvalidAlgorithmParameterException if the <code>keysize</code>
134         * is not equals or geater than 768 and a multible of 256. Where the unit
135         * is bit. Or if the <code>publicExponent</code> isn't F0 or F4.
136         * @see java.security.spec.RSAKeyGenParameterSpec
137         */
138        public void initialize(AlgorithmParameterSpec params, SecureRandom random)
139                throws InvalidAlgorithmParameterException {
140            this.generator.initialize(params, random);
141        } 
142        
143        /**
144         * Generates a <code>KeyPair</code>. Unless an initialization method is
145         * called using a KeyPairGenerator interface, algorithm-specific defaults
146         * will be used. This will generate a new key pair every time it
147         * is called. The defaults are <code>keysize</code> = 768 bit and
148         * random with the <code>SecureRandom</code> implementation of the
149         * highest-priority installed provider as the source of randomness.
150         * (If none of the installed providers supply an implementation of
151         * <code>SecureRandom</code>, a system-provided source of randomness
152         * is used.)
153         *
154         * @return the newly generated <code>KeyPair</code>
155         */
156        public KeyPair generateKeyPair() {    
157            return this.generator.generateKeyPair();
158        }    
159    }